package com.shangs.platform.modular.cms.video.controller;

import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.CmsVideoMapper;
import com.shangs.platform.common.persistence.model.CmsVideo;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.shiro.ShiroKit;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.cms.warpper.VideoWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 视频控制器
 *
 * @author shangs
 * @Date 2017-10-25 15:25:51
 */
@Controller
@RequestMapping("/video")
public class VideoController extends BaseController {

    private String PREFIX = "/cms/video/";

    @Resource
    CmsVideoMapper cmsVideoMapper;

    /**
     * 跳转到视频首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "video.html";
    }

    /**
     * 跳转到添加视频
     */
    @RequestMapping("/video_add")
    public String videoAdd() {
        return PREFIX + "video_add.html";
    }

    /**
     * 跳转到修改视频
     */
    @RequestMapping("/video_update/{videoId}")
    public String videoUpdate(@PathVariable Long videoId, Model model) {
        if (ToolUtil.isEmpty(videoId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsVideo cmsVideo = this.cmsVideoMapper.selectByPrimaryKey(videoId);
        Map<String, Object> map = BeanKit.beanToMap(cmsVideo);
        model.addAttribute("video", map);
        LogObjectHolder.me().set(map);
        return PREFIX + "video_edit.html";
    }

    /**
     * 获取视频列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.cmsVideoMapper.selectByCondition(new CmsVideo());
        return new VideoWarpper(list).warp();
    }

    /**
     * 新增视频
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsVideo video) {
        video.setCreateId(ShiroKit.getUser().getId());
        video.setCreateTime(new Date());
        this.cmsVideoMapper.insertSelective(video);
        return SUCCESS_TIP;
    }

    /**
     * 删除视频
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete (@RequestParam Long videoId) {
        this.cmsVideoMapper.deleteByPrimaryKey(videoId);
        return SUCCESS_TIP;
    }


    /**
     * 修改视频
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid CmsVideo cmsVideo, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        cmsVideo.setUpdateId(ShiroKit.getUser().getId());
        cmsVideo.setUpdateTime(new Date());
        this.cmsVideoMapper.updateByPrimaryKeySelective(cmsVideo);
        return SUCCESS_TIP;
    }

    /**
     * 视频详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Long videoId) {
        if (ToolUtil.isEmpty(videoId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsVideo cmsVideo = this.cmsVideoMapper.selectByPrimaryKey(videoId);
        Map<String, Object> map = BeanKit.beanToMap(cmsVideo);
        return map;
    }
}
